from typing import List, Tuple, Optional
from app.repositories.sponsors_research_repo import SponsorsResearchRepo
from app.schema.sponsors_research_schema import (
    SponsorByGroupItem,
    CompanyContactsItem,
    CompanyScholarshipItem,
    ProgramScholarshipItem
)

class SponsorsResearchService:
    def __init__(self, repo: SponsorsResearchRepo):
        self.repo = repo

    def sponsors_by_group(
        self,
        year_from: int,
        year_to:   int,
        only_current: Optional[bool],
        program_code: Optional[str],
        faculty:      Optional[str],
        coop_program: Optional[str],
        page:   int,
        page_size: int,
    ) -> Tuple[int, List[SponsorByGroupItem]]:
        total, rows = self.repo.count_sponsors_by_group(
            year_from, year_to, only_current,
            program_code, faculty, coop_program,
            page, page_size
        )
        items = [SponsorByGroupItem(**r) for r in rows]
        return total, items

    def list_contacts(
        self,
        year_from:    int,
        year_to:      int,
        only_current: Optional[bool],
        program_code: Optional[str],
        faculty:      Optional[str],
        coop_program: Optional[str],
        page:         int,
        page_size:    int,
    ) -> Tuple[int, List[CompanyContactsItem]]:
        total, rows = self.repo.get_contacts_paginated(
            year_from, year_to, only_current,
            program_code, faculty, coop_program,
            page, page_size
        )
        items = [CompanyContactsItem(**r) for r in rows]
        return total, items

    def get_scholarships_by_company(
        self,
        year_from: int,
        year_to: int,
        only_current: Optional[bool],
        program_code: Optional[str],
        page: int,
        page_size: int,
    ) -> Tuple[int, List[CompanyScholarshipItem]]:
        total, rows = self.repo.sum_scholarships_by_company(
            year_from, year_to, only_current,
            program_code, page, page_size
        )
        items = [CompanyScholarshipItem(**r) for r in rows]
        return total, items

    def get_scholarships_by_program(        
                                    self,
        year_from: int,
        year_to: int,
        only_current: Optional[bool],
        program_code: Optional[str],
        page: int,
        page_size: int,
    ) -> Tuple[int, List[ProgramScholarshipItem]]:
        total, rows = self.repo.sum_scholarships_by_program(
            year_from, year_to, only_current,
            program_code, page, page_size
        )
        items = [ProgramScholarshipItem(**r) for r in rows]
        return total, items